<template>
  <div>

    <el-dialog
      v-loading="loading"
      ref="dialog"
      :show-close="false"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showModelSel"
      :append-to-body="true"
      title="品牌机种选择"
      width="75%"
      @opened="searchHandle"
    >
      <!-- 查询条件 -->
      <el-form ref="searchForm" :inline="true" :model="search" size="mini" >
        <el-form-item prop="brandId" label="品牌">
          <el-select
            v-model="search.brandId"
            placeholder="请选择"
            class="search-input"
            @change="changeBrand(search.brandId)">
            <el-option v-for="item in brandList" :key="item.BRAND_ID" :label="item.BRAND_NAME" :value="item.BRAND_ID">
              {{ item.BRAND_NAME }}
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="seriesId" label="机种">
          <el-select v-model="search.seriesId" placeholder="请选择" class="search-input">
            <el-option
              v-for="item in seriesList"
              :key="item.SERIES_ID"
              :label="item.SERIES_NAME"
              :value="item.SERIES_ID">
              {{ item.SERIES_NAME }}
            </el-option>
          </el-select>
        </el-form-item>
        <div style="text-align:center">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="searchHandle">查询
            </el-button>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-refresh" @click="resetForm('searchForm')">重置
            </el-button>
          </el-form-item>
        </div>
      </el-form>
      <div class="tableHead">
        <div class="tabletitle">品牌机种信息</div>
      </div>
      <!-- 用户列表 -->
      <BaseTable
        ref="table"
        @chaneg-size="changeSizeHandle"
        @chaneg-page="changePageHandle"
        @current-change="select"
        @selection-change="handleSelectionVehicleChange" >
        <el-table-column label="序号" type="index" />
        <el-table-column v-if="allowAudit" type="selection" width="40" />
        <el-table-column prop="DEALER_CODE" label="代理商编码" min-width="150"/>
        <el-table-column prop="BRAND_NAME" label="品牌" min-width="150"/>
        <el-table-column prop="SERIES_NAME" label="机种" min-width="150"/>
      </BaseTable>
      <div style="text-align:center">
        <span slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="confirmHandle" >确 定</el-button>
          <el-button v-show="allowAudit" type="primary" @click="confirmHandleAll" >选择 全部</el-button>
        </span>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getSeries,
  getBrandList,
  getSeriesList
} from '@/api/cars/series'
import filtercode from '@/components/Checkbox/filtercode'
import BaseTable from '@/components/BaseTable'
import formMixins from '@/mixins/formMixins'
import rSelect from '@/components/Select/Select'
export default {
  name: 'SelectSoNo',
  components: {
    BaseTable,
    rSelect,
    filtercode

  },
  mixins: [formMixins],
  props: {
    showModelSel: Boolean,
    allowAudit: Boolean
  },
  data() {
    return {
      loading: false,
      multipleSelection: [],
      search: {
        limit: 10,
        pageNum: 1,
        brandId: '',
        seriesId: '',
        model: '',
        modelCode: '',
        package: ''
      },
      brandList: [],
      seriesList: [],
      seriesdisabled: true,
      modeldisabled: true,
      packagedisabled: true
    }
  },
  watch: {
    showModelSel(val) {
      if (val) {
        this.multipleSelection = []
      }
    }
  },
  mounted() {
    this.getBrands()
    this.searchHandle()
  },
  methods: {
    // 品牌下拉框
    getBrands() {
      getBrandList().then(res => {
        this.brandList = res
      }).catch(err => {
        console.log(err)
      })
    },
    changeBrand(id) {
      this.search.seriesId = ''
      getSeriesList(id).then(res => {
        if (res) {
          //  this.search.seriesId = ""
          //  this.search = JSON.parse(JSON.stringify(this.search))
          this.search = {
            ...this.search,
            seriesId: ''
          }
          this.seriesList = res
        }
      }).catch(err => {
        console.log(err)
      })
    },
    searchHandle() {
      this.search.limit = 5
      const table = this.$refs['table']
      getSeries(this.search)
        .then(res => {
          table.complete().filData(res)
        })
        .catch(err => {
          table.complete()
        })
    },

    cancel() {
      this.$emit('closeModelSel', false)
    },
    // 选择
    select(val) {
      if (val == null) {
        this.multipleSelection = []
      } else {
        this.multipleSelection = val
      }
    },
    handleSelectionVehicleChange(val) {
      if (val == null) {
        this.multipleSelection = []
      } else {
        this.multipleSelection = val
      }
    },
    confirmHandle() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          type: 'warning',
          message: '请选择数据!'
        })
      } else {
        this.$emit('getModelSel', this.multipleSelection)
        this.cancel()
      }
    },
    confirmHandleAll() {
      this.loading = true
      this.search.limit = 10000
      getSeries(this.search)
        .then(res => {
          this.$emit('getModelSel', res.rows)
          this.loading = false
          this.cancel()
        })
        .catch(err => {
          this.$message({
            type: 'warning',
            message: '网络异常'
          })
        })
    }

  }
}
</script>

<style scoped>

.magnifier {
  float: right;
  position: absolute;
  right: 50px;
  top: 23px;
  cursor: pointer;
}
</style>
